package ui.fragment.minefragment;

import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.HashMap;
import java.util.Map;

import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.LoginBean;
import student.andlei.com.student.R;
import ui.activity.login.LoginActivity;
import ui.fragment.MineFragment;
import utils.SPUtils;

/**
 * 重置密码
 */
public class UpdatePasswordFragment extends BaseNetFragment {
    private ImageView iv_back;
    private EditText ed_phone,
                    ed_password,
                    ed_new_password,
                    ed_confirm_new_password;
    private CheckBox checkBox_password,
            checkbox_new_showpassword,
            checkbox_confirm_new_showpassword;
    private Button bt_updatepd_sure;
    public static Fragment getInstance() {
        return new UpdatePasswordFragment();
    }
    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            toast("重置密码成功");
            getActivity().finish();
            jumpTo(LoginActivity.class);
        }else {
            toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_updatepassword;
    }

    @Override
    protected void findViews() {
        iv_back = getView(R.id.img_back);
        ed_phone = getView(R.id.ed_user);
        ed_password = getView(R.id.ed_password);
        ed_new_password = getView(R.id.ed_new_password);
        ed_confirm_new_password = getView(R.id.ed_confirm_new_password);
        checkBox_password = getView(R.id.checkbox_showpassword);
        checkbox_new_showpassword = getView(R.id.checkbox_new_showpassword);
        checkbox_confirm_new_showpassword = getView(R.id.checkbox_confirm_new_showpassword);
        bt_updatepd_sure = getView(R.id.bt_updatepd_sure);
    }

    @Override
    protected void formatViews() {
        iv_back.setOnClickListener(this);
        bt_updatepd_sure.setOnClickListener(this);
        checkBox_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    ed_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    ed_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT );
                }
            }
        });
        checkbox_new_showpassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    ed_new_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    ed_new_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT );
                }
            }
        });
        checkbox_confirm_new_showpassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    ed_confirm_new_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    ed_confirm_new_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT );
                }
            }
        });
    }

    @Override
    protected void formatData() {
        ed_phone.setText(SPUtils.getInstance(getActivity()).getString("user_phone"));
    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_back:
                ((MineFragment)(this.getParentFragment())).GojumpSettingFragment();
                break;
            //重置密码
            case R.id.bt_updatepd_sure:
                Map<String,String> map = new HashMap<>();
                map.put("mobile",ed_phone.getText().toString().trim());
                map.put("password",ed_password.getText().toString().trim());
                map.put("new_password",ed_new_password.getText().toString().trim());
                map.put("confirm_password",ed_confirm_new_password.getText().toString().trim());
                post("common/resetPassword",map, LoginBean.class,true);
                break;

        }
    }
}
